#!/usr/bin/python

import pygtk
import gtk
import gobject

#from launcher import GKVMLauncher
from configparser import GKVMConfigParser

class GKVMGraphic:

	def delete_event(self, widget, event, data=None):
		return False

	def destroy(self, widget, data=None):
		"""docstring for destroy"""
		gtk.main_quit()

	def entry_get_event(self, widget, event, data=None):
		if widget.get_name() == 'GtkButton':
			text  = data[0].get_text()
			count = data[1].get_text()

			if text != None:
				print "Nombre del disco: ", text, count
		else:
			if event.keyval == 65293:
				text  = data[0].get_text()
				count = data[1].get_text()

				if text != None:
					print "Nombre del disco: ", text, count

	def __init__(self):
		"""docstring for __init__"""
		#self.launcher = GKVMLauncher()
		self.config   = GKVMConfigParser()

		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.connect("delete_event", self.delete_event)
		self.window.connect("destroy", self.destroy)
		self.window.set_border_width(10)
		self.window.resize(450,300)

		frame = gtk.Frame(label='Disks')
		self.window.add(frame)

		main_layout = gtk.Layout()
		frame.add(main_layout)

		entry_diskname = gtk.Entry(max=0)
		entry_count = gtk.Entry(max=2)
		entry_diskname.connect('key_press_event', self.entry_get_event, [entry_diskname, entry_count])
		entry_count.connect('key_press_event', self.entry_get_event, [entry_diskname, entry_count])
		main_layout.put(entry_diskname, 10, 10)
		main_layout.put(entry_count, 180, 10)
		
		button_diskname = gtk.Button(label="Create")
		button_diskname.connect('button_press_event', self.entry_get_event, [entry_diskname, entry_count])
		main_layout.put(button_diskname, 360, 9)

		self.listMachines(main_layout)

		self.window.show_all()

	def listMachines(self, main_layout):
		frame_machines = gtk.Frame(label='Machines')
		main_layout.put(frame_machines, 0, 80)

		l = gtk.ListStore(gobject.TYPE_STRING)

		for mm in self.config.machines:
			l.append((mm['name'],))

		treeview = gtk.TreeView()
		treeview.set_model(l)

		column = gtk.TreeViewColumn()
		cell = gtk.CellRendererText()
		column.pack_start(cell)
		column.add_attribute(cell,'text',0)
		treeview.append_column(column)

		treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
		frame_machines.add(treeview)

	def main(self):
		"""docstring for main"""
		gtk.main()

if __name__ == '__main__':
	gkvm = GKVMGraphic()
	gkvm.main()
